package Sort;

import java.util.Arrays;

/**
 * @Classname 希尔排序 就是的插入排序的升级版本
 * @Description TODO
 * @Date 2023/2/17 11:37
 * @Created by xjl
 */
public class 希尔排序 {

    public static boolean gerater(int v, int w) {
        return v - w > 0;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void shellsort(int[] array) {
        // 确定h的初始值
        int h = 1;
        while (h < array.length / 2) {
            h = 2 * h + 1;
        }
        while (h > 1) {
            // 排序
            for (int i = h; i < array.length; i++) {
                // 间隔h的元素
                for (int j = i; j >= h; j -= h) {
                    if (gerater(array[j - h], array[j])) {
                        swap(array, j - h, j);
                    } else {
                        break;
                    }
                }
            }
            // 缩短h的值
            h = h / 2;
        }
    }

    public static void main(String[] args) {
        int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
        shellsort(array);
        System.out.println(Arrays.toString(array));
    }
}
